﻿@model WSCeCommerceWeb.Models.ShoppingCart
@using WSCeCommerce.Domain

@{
    ViewBag.Title = "Shopping Cart";
}

<h2 style="text-align:center">Shopping Cart</h2>

<table id="Cart">
    <tr>
        <th>Item</th>
        <th>Price</th>
        <th>Quantity</th>
        <th>
    </tr>

    @foreach (SaleItem item in Model.GetCartItems())
    {
        <tr id="DisplayCart">
            <td>
                @item.Item.Name
            </td>
            <td>
                @string.Format("{0:#.00}", item.Price)
            </td>
            <td>
                <input type="text" style="width:13px; font-size:11px;" id="UpdateQuantity_@item.Item.ItemId" name="UpdateValue" value="@item.Quantity"/>
                <input type="button" name="UpdateValue" value="Update" id="SubmitQuantity" onclick="update('@item.Item.ItemId    ')"/>
            </td>
            <td>
                <a href="javascript:add(@item.Item.ItemId)">Add</a>
                <a href="javascript:remove(@item.Item.ItemId)">Remove</a>
            </td>
        </tr> 
    }


</table>
<script>

    function add (itemId) {
   
        $.ajax({
            type: "POST",
            url: "/ShoppingCart/AddToCart",
            data: { id: itemId },
            success: function (returndata) {
                if (returndata.ok) {
                     var controlName = "#UpdateQuantity_" + returndata.itemId;
              
                    $(controlName).val(returndata.count);


                }
                else {
                    window.alert(' error : ' + returndata.message);
                }
               

            }
        });
       
    }
 
      function update(itemId) {

        var controlName = "UpdateQuantity_" + itemId;

        var amount = $("#" + controlName).val();
        
        $.ajax({
            type: "POST",
            url: "/ShoppingCart/UpdateQuantity",
            data:
                {
                    id: itemId,
                    Quantity:amount
                },
            success: function (returndata) {
                if (returndata.ok) {
                    $("#quantity").text(returndata.count);
                    window.alert(returndata.message);
                }
                else {
                    window.alert(' error : ' + returndata.message);
                }
            }
             
        }
       );
      }
    
            
    function remove(itemId) {
       
        $.ajax({
            type: "POST",
            url: "/ShoppingCart/RemoveFromCart",
            data: { id: itemId },
            success: function (returndata) {
                if (returndata.ok) {
                    var controlName = "#UpdateQuantity_" + returndata.itemId;
                    $(controlName).val(returndata.count);
                         
                }
                else {
                    window.alert(' error : ' + returndata.message);
                }

            }
        }
 );
    }
</script>
